import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

import config from "@/config";



import createLogger from "vuex/dist/logger"; // 修改日志
import createPersistedState from "vuex-persistedstate"; // 数据持久化
const createPersisted = createPersistedState({
  key: "17wan",

  storage: {
    getItem: key => uni.getStorageSync(key),
    setItem: (key, value) => uni.setStorageSync(key, value),
    removeItem: key => uni.removeStorageSync(key)
  },

  reducer(state) {
    return {
      user: {
        TOKEN: state.user.TOKEN,
        userInfo: state.user.userInfo,
        isLogin: state.user.isLogin,
      },
      guessHero: {
        games: state.guessHero.games,
        type: state.guessHero.type
      },
      setting: {
        config: state.setting.config,
        IS_OPEN_BG_MUSIC: state.setting.IS_OPEN_BG_MUSIC,
        IS_OPEN_GAME_AUDIO: state.setting.IS_OPEN_GAME_AUDIO
      },
      address: {
        data: state.address.data
      }
    };
  }
});
const modulesFiles = require.context("@/store/modules", false, /\.js$/);

const modules = modulesFiles.keys().reduce((modules, modulePath) => {
  const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, "$1");
  const value = modulesFiles(modulePath);
  modules[moduleName] = value.default;
  return modules;
}, {});

const store = new Vuex.Store({
  strict: process.env.NODE_ENV !== 'production',
  plugins: process.env.NODE_ENV !== "production" ? [createPersisted, createLogger] : [createPersisted],
  modules,
  state: {
    THEME: "2d",
    THEMES: {
      poster: "海报",
      cartoon: "卡通",
      void: "虚空",
      "2d": "二次元"
    }
  },
  actions: {

  },
  getters: {
    THEME: state => state.THEME
  },
  mutations: {

  }
});

export default store;
